//
//  YAYIEditNameViewController.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYIEditBriefViewController.h"

@interface YAYIEditBriefViewController () <UITextViewDelegate>

@property (nonatomic, weak) UITextView *editTextView;
@property (nonatomic, weak) UILabel *placeholderLbl;
@property (nonatomic, weak) UILabel *wordsNumberLbl;

@end

@implementation YAYIEditBriefViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人简介";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //
    [self setupNav];
    [self setupSubView];
    
    if (self.brief) {
        self.editTextView.text = self.brief;
        [self textViewDidChange:self.editTextView];
    }
}


- (void)setupNav
{
    // left button
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    rightBtn.bounds = CGRectMake(0, 0, 40, 20);
    [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}


- (void)setupSubView
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.frame = CGRectMake(0, 20, SCREEN_width, 120);
    [self.view addSubview:bgView];
    
    
    UITextView *textView = [[UITextView alloc] init];
    textView.font = [UIFont systemFontOfSize:15];
    textView.delegate = self;
    [bgView addSubview:textView];
    self.editTextView = textView;

    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-30);
    }];
    
    UILabel *placeholderLbl = [[UILabel alloc] init];
    placeholderLbl.text = @"请介绍自己的从业经验和特长，以方便患者了解您。";
    placeholderLbl.numberOfLines = 0;
    placeholderLbl.font = [UIFont systemFontOfSize:15];
    placeholderLbl.textColor = YAYIColor(160, 163, 163);
    [bgView addSubview:placeholderLbl];
    self.placeholderLbl = placeholderLbl;
    [placeholderLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView).offset(5);
        make.right.equalTo(textView).offset(0);
        make.top.equalTo(textView).offset(8);
    }];
    
    UILabel *wordsNumberLbl = [[UILabel alloc] init];
    wordsNumberLbl.text = @"0/200字";
    wordsNumberLbl.font = [UIFont systemFontOfSize:15];
    wordsNumberLbl.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:wordsNumberLbl];
    self.wordsNumberLbl = wordsNumberLbl;
    [wordsNumberLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(textView).offset(-5);
        make.height.equalTo(@30);
        make.bottom.equalTo(bgView).offset(0);
    }];
    
    
    // 线
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = YAYIColor(223, 216, 216);
    [bgView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.width.equalTo(@(SCREEN_width));
        make.height.equalTo(@0.5);
        make.top.equalTo(bgView);
    }];
    
    UIView *lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = YAYIColor(223, 216, 216);
    [bgView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.width.equalTo(@(SCREEN_width));
        make.height.equalTo(@0.5);
        make.bottom.equalTo(bgView);
    }];
    
}


#pragma mark 按钮相应方法
- (void)saveClick:(UIButton *)sender
{
    YAYILog(@"save: indexPath:%ld %ld", self.indexPath.row, self.indexPath.section);

    if (self.editTextView.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"\n\n请上填写内容\n\n"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = self.doctorID;
    param[@"brief"] = self.editTextView.text;
    
    YAYILog(@"parm:%@", param);
    [YAYINetWorking postJsonSerilizeWithURL:me_editUserinfo param:param finish:^(NSData *data, NSDictionary *dict, NSError *error,NSDictionary *responseError) {
        if (error == nil) {
            YAYILog(@"保存标签：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                [SVProgressHUD showWithStatus:@"保存失败"];
                
            }else{
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self backAction];
                    // 执行代理刷新上级控制器
                    if ([self.delegate respondsToSelector:@selector(editBriefDidChanged:)]) {

                        [self.delegate editBriefDidChanged:self.editTextView.text];
                    }
                    
                });
            }
        }else{
            NSString *message = responseError[@"message"];
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];

    
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    
}


#pragma mark - tableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}


#pragma mark textView代理

- (void)textViewDidChange:(UITextView *)textView
{
    YAYILog(@"textViw:%@", textView.text);
    
    if (textView.text.length == 0) {
        self.placeholderLbl.hidden = NO;
    }else{
        self.placeholderLbl.hidden = YES;
    }
    
    // 超过200字就截取
    if (textView.text.length > 200) {
        NSString *text = textView.text;
        text = [text substringToIndex:200];
        textView.text = text;
    }
    
    NSString *wordsNumStr = [NSString stringWithFormat:@"%ld/200字", textView.text.length];
    self.wordsNumberLbl.text = wordsNumStr;
}


- (void)textViewChanged:(UITextView *)textView
{
    YAYILog(@"textViw:%@", textView.text);
    
}



@end
